package com.dh.spring.client.bootstrap;

import com.dh.spring.util.LocalFileSystem;
import com.dh.spring.common.*;
import com.dh.spring.util.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;

/**
 * @Auther: YSFY
 * @Date: 2019/11/30 19:28
 * @Pacage_name: com.dh.spring.client.bootstrap
 * @Project_Name: arthas-client
 * @Description: NIO客户端http请求监听代理
 */

@Slf4j
@SuppressWarnings("all")
public class GlobalNIOClientListenerDelegate {
    private static final Logger logger = LoggerFactory.getLogger(GlobalNIOClientListenerDelegate.class);
    public static String SOCKET_CLIENT_IP = "127.0.0.1", SOCKET_REMOTE_IP = "127.0.0.1", FILE_UPLOAD_PATH = BasicConstant.DATA_EMPTY, FILE_DOWNLOAD_PATH = BasicConstant.DATA_EMPTY;
    public static int SOCKET_CLIENT_PORT = -1, SOCKET_REMOTE_PORT = -1, SOCKET_BUFFER_SIZE = 1024, SOCKET_REMOTE_COMMAND_PORT = -1;
    public static LocalFileSystem localUploadFileSystem = null, localDownloadFileSystem = null;
    private static PropertiesUtil pro = null;

    static {
        try {
            pro = new PropertiesUtil();
            SOCKET_CLIENT_PORT = Integer.parseInt(pro.getValue(BasicConstant.SOCKET_CLIENT_PORT));
            SOCKET_CLIENT_IP = pro.getValue(BasicConstant.SOCKET_CLIENT_IP);
            SOCKET_REMOTE_PORT = Integer.parseInt(pro.getValue(BasicConstant.SOCKET_REMOTE_PORT));
            SOCKET_REMOTE_COMMAND_PORT = Integer.parseInt(pro.getValue(BasicConstant.SOCKET_REMOTE_COMMAND_PORT));
            SOCKET_REMOTE_IP = pro.getValue(BasicConstant.SOCKET_REMOTE_IP);
            //SOCKET_BUFFER_SIZE = Integer.parseInt(pro.getValue(BasicConstant.SOCKET_BUFFER_SIZE));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void acceptSocket() {
        try {
            //TODO 判断监听IP地址是否符合
            if(!BasicUtil.analysisCurrentServerIpService(SOCKET_CLIENT_IP)) {
                log.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] GlobalNIOClientListenerDelegate | --> 客户端可用IP地址为空或与配置文件的IP地址不匹配,文件客户端服务启动失败");
                return;
            }

            GlobalNIOClientBootstrap.start();
        } catch (Exception e) {
            log.error("[" + LocalTime.formatDate(LocalDateTime.now()) + "] GlobalNIOClientListenerDelegate | --> 文件客户端启动异常, error = {}", e.getMessage());
            return;
        }
    }
}
